﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ThridGroup.ERP.DTO.ProductionSystem;
using ThridGroup.ERP.HumanResources;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace ThridGroup.ERP.ProductionSystem
{
    public class ProductionWasteOrderServices : ApplicationService, IProductionWasteOrderServices
    {
        private readonly IRepository<ProductionWasteOrder, int> _productionWasteOrder;

        public ProductionWasteOrderServices(IRepository<ProductionWasteOrder, int> productionWasteOrder)
        {
            _productionWasteOrder = productionWasteOrder;
        }

        public async Task<int> AddProductionWasteOrder(ProductionWasteOrderDTO productionWasteOrder)
        {
            var entity = ObjectMapper.Map<ProductionWasteOrderDTO, ProductionWasteOrder>(productionWasteOrder);
            var result = await _productionWasteOrder.InsertAsync(entity);
            return result == null ? 0 : 1;
        }

        public async Task DelProductionWasteOrder(List<int> ids)
        {
            await _productionWasteOrder.DeleteDirectAsync(m => ids.Contains(m.Id));
        }

        public async Task<ProductionWasteOrderDTO> GetProductionWasteOrder(int id)
        {
            var productionWasteOrder = await _productionWasteOrder.FindAsync(m => m.Id == id);
            var result = ObjectMapper.Map<ProductionWasteOrder, ProductionWasteOrderDTO>(productionWasteOrder);
            return result;
        }

        public async Task<List<ProductionWasteOrderDTO>> ShowProductionWasteOrder(int pageIndex = 1, int pageSize = 10, string? wasteOrderName = "")
        {
            var list = await _productionWasteOrder.GetListAsync();
            list = list.WhereIf(!string.IsNullOrEmpty(wasteOrderName), m => m.WasteOrderName.Contains(wasteOrderName)).ToList();
            var result = ObjectMapper.Map<List<ProductionWasteOrder>, List<ProductionWasteOrderDTO>>(list);
            return result;
        }

        public async Task<int> UpdateProductionWasteOrder(ProductionWasteOrderDTO productionWasteOrder)
        {
            var entity = ObjectMapper.Map<ProductionWasteOrderDTO, ProductionWasteOrder>(productionWasteOrder);
            var result = await _productionWasteOrder.UpdateAsync(entity);
            return result == null ? 0 : 1;
        }
    }
}
